﻿using System;
using System.Linq;
using System.Management;
using ClusterAware.Properties;
using ClusterAwareProperties;
using ClusterAwareWMI;

namespace ClusterAware
{
    /// <summary>
    /// Wrapper property to Node Class actions
    /// </summary>
    public class NodeMethods
    {

        #region private properties

        private readonly ClusterManager _managerclass;

        #endregion

        #region Ctor

        /// <summary>
        /// private Constructor
        /// </summary>
        private NodeMethods()
        {
        }

        /// <summary>
        /// Create Class NodeMethods
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public NodeMethods(ClusterManager managerclass)
            : this()
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        /// <summary>
        /// CusterManager Instance
        /// </summary>
        public ClusterManager ManagerInstance
        {
            get { return _managerclass; }
        }

        /// <summary>
        /// if Exists Node
        /// </summary>
        /// <param name="nodeName">Node name</param>
        /// <returns>True if exists</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Node name is empty
        /// </exception>
        public bool ExistNode(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            if (_managerclass.CacheInstances)
            {
                return _managerclass.NodeInstances.ContainsKey(nodeName);
            }
            try
            {
                var node = new ClusterWmiHelpper().GetNode(nodeName, _managerclass.ScopeManager);
                return (node != null);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// get list of Nodes
        /// </summary>
        /// <returns>Array of class Nodes</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected,
        /// Failed WMI method execution
        /// </exception>
        public NodeProperty[] GetNodes()
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            try
            {
                return _managerclass.CacheInstances ? _managerclass.NodeInstances.Values.ToArray() : new ClusterWmiHelpper().GetNodes(_managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.GetNodes", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Nodes.GetNodes", ex.Message), ex);
            }
        }
        /// <summary>
        /// Get a Node
        /// </summary>
        /// <param name="nodeName">name Node</param>
        /// <returns>Node class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Node name is empty, 
        /// Node not exists,
        /// Failed WMI method execution
        /// </exception>
        public NodeProperty GetNode(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.NodeInstances.ContainsKey(nodeName))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Node", nodeName));
                }
                return _managerclass.NodeInstances[nodeName];
            }
            try
            {
                return new ClusterWmiHelpper().GetNode(nodeName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Nodes.GetNode", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Nodes.GetNode", ex.Message), ex);
            }
        }

        ///<summary>
        /// Save a Node in Cluster
        ///</summary>
        ///<param name="nodeClass">Node CLass to Save</param>
        /// <returns>
        /// NodeProperty Class
        /// </returns>
        public NodeProperty Save(NodeProperty nodeClass)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (nodeClass == null)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "Nodeclass"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.NodeInstances.ContainsKey(nodeClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Node", nodeClass.Name));
                }
            }
            else
            {
                if (!ExistNode(nodeClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Node", nodeClass.Name));
                }
            }
            try
            {
                nodeClass.SaveManagementObject(_managerclass.OperationSystem);
                var ret = new ClusterWmiHelpper().GetNode(nodeClass.Name, _managerclass.ScopeManager);
                if (_managerclass.CacheInstances)
                {
                    _managerclass.NodeInstances[nodeClass.Name] = ret;
                }
                return ret;
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Save", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Save", ex.Message), ex);
            }
        }

        /// <summary>
        /// Get list active Resource of a Node
        /// </summary>
        /// <param name="nodeName">name Node</param>
        /// <returns>Array resource class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Node not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public ResourceProperty[] GetActiveResources(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindActiveResourcesByNode(nodeName, _managerclass.ScopeManager);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.GetActiveResources", ex.Message), ex);
            }
        }

        /// <summary>
        /// Get list active group of a Node
        /// </summary>
        /// <param name="nodeName">name Node</param>
        /// <returns>Array group class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Node not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public GroupProperty[] GetActiveGroups(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindActiveGroupsByNode(nodeName, _managerclass.ScopeManager);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.GetActiveGroups", ex.Message), ex);
            }
        }


        /// <summary>
        /// Get list networkInterface of a Node
        /// </summary>
        /// <param name="nodeName">name Node</param>
        /// <returns>Array networkInterface class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Node not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public NetworkInterfaceProperty[] GetNetworkInterfaces(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindNetworkInterfacesByNode(nodeName, _managerclass.ScopeManager);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.GetNetworkInterfaces", ex.Message), ex);
            }
        }

        #endregion
    }
}
